package org.aplombh.java.utils.multiThread.deathLock;

public class DeathLock extends Thread {

    public static void main(String[] args) {
        DeathLock cinderella = new DeathLock(0, "Cinderella");
        DeathLock snowWhite = new DeathLock(1, "Snow White");

        snowWhite.start();
        cinderella.start();

    }


    static Mirror mirror = new Mirror();
    static Lipstick lipstick = new Lipstick();

    int choice;
    String name;

    public DeathLock(int choice, String name) {
        this.choice = choice;
        this.name = name;
    }

    @Override
    public void run() {
        try {
            makeup();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private void makeup() throws InterruptedException {
        if (choice == 0) {
            synchronized (lipstick) {
                System.out.println(this.name + " get lipstick;");
                Thread.sleep(1000);
                System.out.println("One second," + this.name + "want to get mirror;");
                synchronized (mirror) {
                    System.out.println(this.name + " get mirror;");
                }
            }
        }else
            synchronized (mirror) {
                System.out.println(this.name + " get mirror;");
                Thread.sleep(1000);
                System.out.println("One second," + this.name + "want to get lipstick;");
                synchronized (lipstick) {
                    System.out.println(this.name + " get lipstick;");
                }
            }
    }
}

